package com.abbas.smartCity.ui;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ListView;

import com.abbas.smartCity.App;
import com.abbas.smartCity.R;
import com.abbas.smartCity.adapter.ListCommAdapter;
import com.abbas.smartCity.base.BaseFragment;
import com.abbas.smartCity.bean.ServerBean;
import com.abbas.smartCity.retroft.ICallback;
import com.abbas.smartCity.ui.findJob.FindJobActivity;
import com.abbas.smartCity.ui.govservice.GovSerMainActivity;
import com.abbas.smartCity.ui.movie.MovieActivity;
import com.abbas.smartCity.ui.parkingWhere.ParkingMainActivity;
import com.abbas.smartCity.ui.youthInn.YouthInnMainActivity;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import retrofit2.Call;

/**
 * 全部服务碎片
 */
public class ServerFragment extends BaseFragment {

    private ListView lv;//服务类型列表
    private GridView gv;//对应服务类型的服务列表
    private ArrayList<String> serverType;//服务类型
    private List<ServerBean.RowsBean> rows;//服务
    private Map<String, List<ServerBean.RowsBean>> serverTypeMap;//已分类的服务数据

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_server;
    }

    @Override
    protected void initView() {
        lv = (ListView) rootView.findViewById(R.id.lv);
        gv = (GridView) rootView.findViewById(R.id.gv);
    }

    @Override
    protected void initData() {
        //服务数据
        api.getServer().enqueue(new ICallback<ServerBean>() {
            @Override
            public void onSuccess(Call<ServerBean> call, ServerBean data) {
                rows = data.getRows();

                //服务分组
//                serverTypeMap = rows.stream()
//                        .collect(Collectors.groupingBy(ServerBean.RowsBean::getServiceType));
                serverTypeMap = rows.stream()
                        .collect(Collectors.groupingBy(rowsBean -> {
                            if (!TextUtils.isEmpty(rowsBean.getServiceType())){
                                return rowsBean.getServiceType();
                            }else {
                                return "老六官方";
                            }
                        }));
                //服务类别集合
                serverType = new ArrayList<>(serverTypeMap.keySet());
                //额外添加一个 全部服务
                serverType.add(0,"全部服务");
                //服务类别列表数据设置
                lv.setAdapter(new ArrayAdapter<>(requireContext(),android.R.layout.simple_list_item_1, serverType));
                //显示所有服务
                showServer(rows);
                //第二种选择
//        showServer("全部服务");
            }
        });

    }

    //各个模块集合 string-->模块名称 class-->Activity类
    private static final Map<String,Class<?>> activityMap = new HashMap<>();

    //初始化模块集合
    static {
        activityMap.put("青年驿站", YouthInnMainActivity.class);
        activityMap.put("政府热线", GovSerMainActivity.class);
        activityMap.put("找工作", FindJobActivity.class);
        activityMap.put("看电影", MovieActivity.class);
        activityMap.put("停哪儿", ParkingMainActivity.class);

    }

    @Override
    protected void initEvent() {
        //服务类型点击
        lv.setOnItemClickListener((adapterView, view, i, l) -> {
            //showServer(serverType.get(i));
            //若是全部服务则显示全部数据
            if ("全部服务".equals(serverType.get(i))){
                showServer(rows);
            }else {
                //否则筛选出对应类型的服务数据
                showServer(serverTypeMap.get(serverType.get(i)));
            }
        });

        //服务表格点击
        gv.setOnItemClickListener((adapterView, view, i, l) -> {
            //跳转到指定页面
            final ServerBean.RowsBean rowsBean = rows.get(i);
            //判空
            final Class<?> targetActivity = activityMap.get(rowsBean.getServiceName());
            if (targetActivity != null) {
                startActivity(targetActivity);
            }
        });
    }

    /**
     * 方案一 传入服务类别名称后进行筛选，将筛选结果展示出来
     * @param name 服务名称
     */
    private void showServer(String name){
        List<ServerBean.RowsBean> resultList;
        if ("全部服务".equals(name)){
            resultList = rows;
        }else {
            resultList = rows.stream()
                    .filter(data -> TextUtils.equals(data.getServiceType(), name))
                    .collect(Collectors.toList());
        }

        gv.setAdapter(new ListCommAdapter<ServerBean.RowsBean,ListCommAdapter.ServerHolder>(
            resultList,
                ListCommAdapter.ServerHolder.class,
                R.layout.server_item
        ) {
            @Override
            protected void show(ServerBean.RowsBean data, View convertView, ServerHolder viewHolder, int position, Context context) {
                Glide.with(context)
                        .load(App.getBaseUrl() + data.getImgUrl())
                        .into(viewHolder.im);
                viewHolder.tv.setText(data.getServiceName());
            }
        });
    }

    /**
     * 方案二 直接传入需要显示的服务数据并显示
     * @param data 服务数据
     */
    private void showServer(List<ServerBean.RowsBean> data){
        gv.setAdapter(new ListCommAdapter<ServerBean.RowsBean,ListCommAdapter.ServerHolder>(
                data,
                ListCommAdapter.ServerHolder.class,
                R.layout.server_item
        ) {
            @Override
            protected void show(ServerBean.RowsBean data, View convertView, ServerHolder viewHolder, int position, Context context) {
                Glide.with(context)
                        .load(App.getBaseUrl() + data.getImgUrl())
                        .into(viewHolder.im);
                viewHolder.tv.setText(data.getServiceName());
            }
        });
    }








}